package com.beep.erp.model.stock;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.beep.erp.R;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Jolly on 2016/11/9.
 */

public class StockAdapter extends RecyclerView.Adapter<StockAdapter.StockViewHolder> {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private OnItemClickLitener mOnItemClickLitener;
    private String[] mList;
    private int[] mImages;

    public StockAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        setDate();
    }

    private void setDate() {
        mList = mContext.getResources().getStringArray(R.array.stock_list_arrays);
        mImages = new int[]{R.mipmap.ic_pad_purchase_paid, R.mipmap.ic_pad_purchase_paid,
                R.mipmap.ic_pad_purchase_paid, R.mipmap.ic_pad_purchase_paid,
                R.mipmap.ic_pad_purchase_paid};
    }

    public void setOnItemClickLitener(StockAdapter.OnItemClickLitener onItemClickLitener) {
        this.mOnItemClickLitener = onItemClickLitener;
    }

    @Override
    public StockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new StockViewHolder(mLayoutInflater.inflate(R.layout.stock_list_item, parent, false));
    }


    @Override
    public void onBindViewHolder(StockViewHolder holder, int position) {
        holder.textView.setText(mList[position]);
        holder.imageView.setImageResource(mImages[position]);
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(view -> mOnItemClickLitener.onItemClick(view, holder.getAdapterPosition()));
        }
    }

    @Override
    public int getItemCount() {
        return mList.length;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    static class StockViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_title)
        ImageView imageView;
        @BindView(R.id.tv_title)
        TextView textView;

        StockViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
